package com.zfzs.post.modules.transaction.service.impl;

import com.alibaba.fastjson.JSON;
import com.zfzs.post.model.vo.TransactionSumVO;
import com.zfzs.post.modules.transaction.domain.TransactionInfo;
import com.zfzs.post.modules.transaction.repository.TransactionInfoRepository;
import com.zfzs.post.modules.transaction.service.TransactionInfoService;
import com.zfzs.post.modules.transaction.service.dto.*;
import com.zfzs.post.modules.transaction.service.mapper.TransactionInfoMapper;
import com.zfzs.post.modules.transaction.service.mybatis.IbatisTransactionService;
import com.zfzs.post.utils.PageUtil;
import com.zfzs.post.utils.QueryHelp;
import com.zfzs.post.utils.ValidationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * 终端交易实现类
 *
 * @author ck
 * @date 2019-10-08
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class TransactionInfoServiceImpl implements TransactionInfoService {

    @Autowired
    private TransactionInfoRepository transactionInfoRepository;

    @Autowired
    private IbatisTransactionService ibatisTransactionService;

    @Autowired
    private TransactionInfoMapper transactionInfoMapper;


    @Override
    public Object queryAll(TransactionInfoQueryCriteria criteria, Pageable pageable) {
        Page<TransactionInfo> page = transactionInfoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable);
        return PageUtil.toPage(page.map(transactionInfoMapper::toDto));
    }

    @Override
    public Object queryAll(TransactionInfoQueryCriteria criteria) {
        return transactionInfoMapper.toDto(transactionInfoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder)));
    }

    @Override
    public TransactionInfo findById(Long transactionId) {
        Optional<TransactionInfo> transactionInfo = transactionInfoRepository.findById(transactionId);
        return transactionInfo.get();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public TransactionInfo create(TransactionInfo resources) {
        resources.setCreateTime(new Date());
        return transactionInfoRepository.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(TransactionInfo resources) {
        Optional<TransactionInfo> optionalTransactionInfo = transactionInfoRepository.findById(resources.getTransactionId());
        ValidationUtil.isNull(optionalTransactionInfo, "TransactionInfo", "id", resources.getTransactionId());
        TransactionInfo transactionInfo = optionalTransactionInfo.get();
        transactionInfo.copy(resources);
        transactionInfoRepository.save(transactionInfo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long transactionId) {
        transactionInfoRepository.deleteById(transactionId);
    }

    @Override
    public BigDecimal queryTransactionSumByDate(Long id, String startDate, String endDate) {
        return transactionInfoRepository.getTransactionSumByDate(id, startDate, endDate);
    }

    @Override
    public int queryTransactionCountByDate(Long id, String startDate, String endDate) {
        return transactionInfoRepository.getTransactionCountByDate(id, startDate, endDate);
    }

    @Override
    public BigDecimal querySumByAgentIds(Long channelId, List<Long> ids, String startDate, String endDate) {
        return transactionInfoRepository.querySumByAgentIds(channelId,ids,startDate,endDate);
    }

    @Override
    public TransactionSumVO queryTransactionSum(Long channelId, List<Long> ids, String startDate, String endDate) {
        Map<String,Object> map = transactionInfoRepository.queryTransactionSum(channelId,ids,startDate,endDate);
        TransactionSumVO vo = JSON.parseObject(JSON.toJSONString(map), TransactionSumVO.class);
        return vo;
    }

    @Override
    public List<TerAvgTransactionDTO> findTerAvgTransaction(Map<String, Object> params) {
        return ibatisTransactionService.findTerAvgTransaction(params);
    }

    @Override
    public List<TerSumTransactionDTO> findTerSumTransaction(Map<String, Object> params) {
        return ibatisTransactionService.findTerSumTransaction(params);
    }

    @Override
    public List<TransactionInfo> queryNotBenefitTransaction(String tranDate,Long channelId){
        return transactionInfoRepository.findByDateAndChannelId(tranDate,channelId);
    }

    @Override
    public List<SumTranByLevelDTO> findSumTranGroupByLevel(Map<String, Object> params) {
        return ibatisTransactionService.findSumTranGroupByLevel(params);
    }

    @Override
    public List<SumTranByTypeDTO> findSumTranGroupByType(Map<String, Object> params) {
        return ibatisTransactionService.findSumTranGroupByType(params);
    }
}